<?php
/**
 * 会员日志
 */
namespace App\Http\Controllers;

use App\Validation\CommentAppValidation;
use App\Service\CommentService;
use App\Validation\CommentValidation;
use App\Validation\DeleteValidation;

class CommentController extends BaseController
{

	/**
	 * [indexAction 列表]
	 * p:当前页数
	 * page_num:每页数量
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser(0);
		$raw = $this->validation(new CommentAppValidation, $this->request->getJsonRawBody());
		$raw->p = 1;
		$raw->page_num = 1000;

		$data = CommentService::getList($raw);
		$d = [];
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->avatar = $value->avatar ? $this->getFullUrl($value->avatar, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			$d[$key]->sign_id = $value->up_id;
			// $d[$key]->create_time = date("Y-m-d", strtotime($value->create_time));
		}
		$count = CommentService::getList($raw, TRUE);
		
		$assign = [
			'total' => $count,
			'list' => $d,
		];
        
		return $this->echoJson($assign);
	}

	/**
	 * [doAction 评论、回复]
	 * @return [type] [description]
	 */
	public function doAction()
	{
		$this->getUser();
		$raw = $this->validation(new CommentValidation, $this->request->getJsonRawBody());
		$data = CommentService::save($raw);
		return $this->echoJson($data);
	}

	public function detailAction()
	{
		$this->getUser();
		$id = $this->dispatcher->getParam('id');
		$data = CommentService::getInfo($id);
		return $this->echoJson($data);
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = CommentService::delete($raw->id, $raw->member_id);

		if ($data === -1)
			return $this->echoJson('不能删除不是自己的评论');

		return $this->echoJson('ok');
	}

}